package com.bawei.liliquan1409f20160620;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {


	private Button button;
	private ListView listview;
    private List<News> newss;
	private NewsAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button = (Button) findViewById(R.id.button);
		listview = (ListView) findViewById(R.id.listview);
		button.setOnClickListener(this);
	    adapter = new NewsAdapter(MainActivity.this,newss);
		listview.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
	  switch (v.getId()) {
	case R.id.button:
		 huoqu();
		break;

	default:
		break;
	}
		
	}

	private void huoqu() {
		
		new  AsyncTask<String,Integer,String>(){

			@Override
			protected String doInBackground(String... params) {
				try {
					URL url=new URL(params[0]);
					HttpURLConnection connection=(HttpURLConnection) url.openConnection();
					connection.setConnectTimeout(5000);
					connection.setReadTimeout(5000);
					connection.setRequestMethod("GET");
					connection.connect();
					int code=connection.getResponseCode();
					if(code==200){
						InputStream stream=connection.getInputStream();
						BufferedReader reader=new BufferedReader(new InputStreamReader(stream));
						String readline;
						StringBuffer buffer=new StringBuffer();
						while((readline=reader.readLine())!=null){
							
							buffer.append(readline);
							
							
							
							
						}
						
						return buffer.toString();
						
						
					}
					
					
					
					
					
					
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
				return null;
			}
			protected void onPostExecute(String result) {
				
				List<News> news=jiexi(result);
				newss.clear();
				newss.addAll(news);
				adapter.notifyDataSetChanged();
				 
				
				
			};
			
			
			
			
			
		}.execute("http://101.200.142.201:8080/tqyb/movie.json");
		
		
		
	}
  private List<News> jiexi(String json){
	  List<News> news=new ArrayList<News>();
	  try {
		JSONObject obj=new JSONObject(json);
		JSONObject jsonObject=obj.getJSONObject("param");
		JSONArray array=jsonObject.getJSONArray("result");
		for(int i=0;i<array.length();i++){
		JSONObject ject=array.getJSONObject(i);	
			News neww=new News();
			neww.setAct(ject.getString("act"));
			neww.setArea(ject.getString("ject"));
			neww.setCover(ject.getString("cover"));
			neww.setDesc(ject.getString("desc"));
			neww.setDir(ject.getString("dir"));
			neww.setTag(ject.getString("tag"));
			neww.setTitle(ject.getString("title"));
			neww.setType(ject.getString("type"));
			neww.setUpdata_num(ject.getString("updata_num"));
			neww.setVid(ject.getString("vid"));
			neww.setYear(ject.getString("year"));
			news.add(neww);
			
			
		}
		
		
		
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  
	  
	  
	  
	  
	  return news;
  }
	
}
